using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using FinanceStats.Entities;
using FinanceStats.Business;

namespace FinanceStats
{
    public partial class Resolver : Form
    {
        private int userID = 0;
        public Resolver(int userID)
        {
            this.userID = userID;
            InitializeComponent();
        }

        public void SetComponent(Control control)
        {
            this.Controls.Add(control);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button6_Click(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {
            DataGridView dgw = this.Controls["dataGridNotMatched"] as DataGridView;
            if (dgw.SelectedRows.Count > 0)
            {
                List<UserOutcome> outcomeItems = new List<UserOutcome>();
                List<Importitem> importItems = new List<Importitem>();
                List<Importitem> gridItems = ((BindingSource)dgw.DataSource).DataSource as List<Importitem>;

                for (int i = 0; i < dgw.SelectedRows.Count; i++)
                {
                    UserOutcome ou = new UserOutcome();
                    ou.ItemID = ((OutcomeItem)((ComboBox)this.Controls["cmbItem"]).SelectedItem).ItemID;
                    Importitem ii = gridItems[i];
                    importItems.Add(ii);
                    DataGridViewRow row = dgw.SelectedRows[i];
                    ou.UserID = this.userID;
                    ou.Date = (DateTime)row.Cells["Date"].Value;
                    ou.Amount = (decimal)row.Cells["Amount"].Value;
                    outcomeItems.Add(ou);
                }
                bool sucess = DataManager.Instance.InsertUserOutcomeList(outcomeItems);
                if (!sucess)
                {
                    MessageBox.Show(this, "Error saving user outcomme items", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    sucess = DataManager.Instance.DeleteListImportitem(importItems);
                }
                for (int i = 0; i < importItems.Count; i++)
                {
                    gridItems.Remove(importItems[i]);
                }
                ((BindingSource)dgw.DataSource).ResetBindings(false);
            }
        }
    }
}